package com.beibei.framework.logger;

import java.text.MessageFormat;

import org.apache.log4j.Level;
import org.apache.log4j.LogManager;

/**
 * 日志接口
 * 
 * @author: 309814490@qq.com
 * @version: 2014-02-12
 */
public class LoggerDefaultImpl implements ILogger {
	private static MessageFormat _msgIdFormat = new MessageFormat("[{0}] [{1}]:{2}");
	private static MessageFormat _msgFormat = new MessageFormat("[{0}]:{1}");
	private String _loggerType;
	private org.apache.log4j.Logger _logImpl;

	public LoggerDefaultImpl(String loggerType) {
		this._loggerType = loggerType;
		this._logImpl = LogManager.getLogger(this._loggerType);
	}

	public LoggerDefaultImpl(org.apache.log4j.Logger logImpl) {
		this._logImpl = logImpl;
	}

	public LoggerDefaultImpl(String loggerType, org.apache.log4j.Logger logImpl) {
		this._loggerType = loggerType;
		this._logImpl = logImpl;
	}

	public String getLoggerType() {
		return this._loggerType;
	}

	public boolean debug(String method, String msg) {
		return debug(method, msg, (Throwable) null);
	}

	public boolean debug(String method, String userId, String msg) {
		return debug(method, userId, msg, null);
	}

	public boolean debug(String method, String msg, Throwable ex) {
		if (this._logImpl.isEnabledFor(Level.DEBUG)) {
			if (null != ex)
				this._logImpl.debug(format(this._loggerType, method, msg), ex);
			else
				this._logImpl.debug(format(this._loggerType, method, msg));
			return true;
		}
		return false;
	}

	public boolean debug(String method, String userId, String msg, Throwable ex) {
		if (this._logImpl.isEnabledFor(Level.DEBUG)) {
			if (null != ex)
				this._logImpl.debug(format(this._loggerType, method, userId, msg), ex);
			else
				this._logImpl.debug(format(this._loggerType, method, userId, msg));
			return true;
		}
		return false;
	}

	public boolean info(String method, String msg) {
		return info(method, msg, (Throwable) null);
	}

	public boolean info(String method, String userId, String msg) {
		return info(method, userId, msg, null);
	}

	public boolean info(String method, String msg, Throwable ex) {
		if (this._logImpl.isEnabledFor(Level.INFO)) {
			if (null != ex)
				this._logImpl.info(format(this._loggerType, method, msg), ex);
			else
				this._logImpl.info(format(this._loggerType, method, msg));
			return true;
		}
		return false;
	}

	public boolean info(String method, String userId, String msg, Throwable ex) {
		if (this._logImpl.isEnabledFor(Level.INFO)) {
			if (null != ex)
				this._logImpl.info(format(this._loggerType, method, userId, msg), ex);
			else
				this._logImpl.info(format(this._loggerType, method, userId, msg));
			return true;
		}
		return false;
	}

	public boolean error(String method, String msg) {
		return error(method, msg, (Throwable) null);
	}

	public boolean error(String method, String userId, String msg) {
		return error(method, userId, msg, null);
	}

	public boolean error(String method, String msg, Throwable ex) {
		if (this._logImpl.isEnabledFor(Level.ERROR)) {
			if (null != ex)
				this._logImpl.error(format(this._loggerType, method, msg), ex);
			else
				this._logImpl.error(format(this._loggerType, method, msg));
			return true;
		}
		return false;
	}

	public boolean error(String method, String userId, String msg, Throwable ex) {
		if (this._logImpl.isEnabledFor(Level.ERROR)) {
			if (null != ex)
				this._logImpl.error(format(this._loggerType, method, userId, msg), ex);
			else
				this._logImpl.error(format(this._loggerType, method, userId, msg));
			return true;
		}
		return false;
	}

	public boolean file(String message) {
		this._logImpl.info(message);
		return true;
	}
	
	private String format(String loggerType, String method, String userId,
			String msg) {
		return _msgIdFormat.format(new Object[] { method, userId, msg });
	}

	private String format(String loggerType, String method, String msg) {
		return _msgFormat.format(new Object[] { method, msg });
	}

}